﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Runtime.Serialization;


namespace Qplus.protocol
{
    /* 定义抽象类，不能实例化*/
    public abstract class TCPPackage : System.Object
    {
        /**
	    * 消息id
	    */
        protected int msgID =0;

        /**
	     * 对应的请求id，仅限inpacket
	     */
        protected int replyID;

        /**
	    * 此包是否需要等待服务器回复，仅限outpacket
	    */
        protected bool needReply = false;


        /**
	     * 此包是否已发送出去
	     */
        protected Boolean IsSend = false;

        /**
	    * 设置消息id
	    * @param msgID
	    */

        public int getMsgID()
        {
            return msgID;
        }

        public void setMsgID(int msgID)
        {
            this.msgID = msgID;
        }

        public TCPPackage() { }

        public void fillHead(byte[] data, int len)
        {
            byte[] head = ProtocolUtil.genMessageHead(0, len, msgID);  //当前消息不加密
            data[0] = head[0];
            data[1] = head[1];
            data[2] = head[2];
            data[3] = head[3];
            data[4] = head[4];
        }

        protected int getHeadLen()
        {
            return 5;
        }

        public Boolean isNeedReply()
        {
            return needReply;
        }

        /**
	    * 设置对应的请求id
	    * @param msgID
	    */
        public void setReplyID(int msgID)
        {
            this.replyID = msgID;
        }

        public int getReplyID()
        {
            return replyID;
        }

        public Boolean isSend()
        {
            return this.IsSend;
        }

        public void setSend(Boolean isSend)
        {
            this.IsSend = isSend;
        }

        /**
	    * 处理服务器返回消息
	    * @param data
	    * @param offset
	    * @param count
	    */
	    //protected abstract void handle(byte[] data, int offset, int count);


        public object Clone()
        {
            //return this as object;      //引用同一个对象
            //return this.MemberwiseClone(); //浅复制
            //return new TCPPackage() as object;//深复制
            return this.MemberwiseClone();//low
            //return this.DataContractSerializer();//deep
        }

    }
}
